//************************************************************
//    > File Name: happy-number.cpp
//    > Author: davis
//    > Mail:  
//    > Created Time: 2017年03月24日 15:56:37
//***********************************************************
//lintcode #488
#include<bits/stdc++.h>
using namespace std;
int getNext(int);
bool isHappy(int n){
	unordered_set<int> visit;
	while(n!=1 && !visit.count(n)){
		visit.emplace(n);
		n=getNext(n);
	}
	return n==1;
}
int getNext(int n){
	int sum=0;
	while(n!=0){
		sum+=pow(n%10,2);
		n /= 10;
	}
	return sum;
}
int main(){
	cout<<"please enter a number:";
	int n;
	cin>>n;
	if(isHappy(n))
		cout<<n<<" is a happy number"<<endl;
	else
		cout<<n<<" is not a happy number"<<endl;
	return 0;
}
